package com.rou.im.framework.core.log;

/**
 * <p></p>
 *
 * @author GaoShuangWen
 * @since 2020-05-13 22:51
 */

import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class LOG {
    /**
     * 日志文件根目录
     */
    public static String LOCAL_LOG_ROOT = "log/";


    private static boolean debug = false;
    /**
     * 输出的标准输出标识，true 输出，false不输出
     */
    private static boolean output2Stdout = false;

    /**
     * 保存日志组件名字创建后不再创建
     */
    private static Map<String,JLogBase>  mapName=new  ConcurrentHashMap<>();

    public static boolean isOutput2Stdout() {
        return output2Stdout;
    }

    public static void setOutput2Stdout(boolean output2Stdout) {
        LOG.output2Stdout = output2Stdout;
    }

    private static void getThreadStatus(JLogBase log, StringBuilder sb) {
        if (log != null) {
            sb.append("\t").append(log.getName()).append("\t").append(log.getState().toString()).append("\r\n");
        }
    }


    public static void state(StringBuilder sb) {
        sb.append("LOG theads running status:\r\n");
        getThreadStatus(debugLog, sb);
        getThreadStatus(errorLog, sb);
//		getThreadStatus(runLog, sb);
//		getThreadStatus(cache_hit_miss, sb);
//		getThreadStatus(syncLog, sb);
//		getThreadStatus(statLog, sb);
//		getThreadStatus(recvLog, sb);
//		getThreadStatus(downloadLog, sb);
//		getThreadStatus(successLog, sb);
//		getThreadStatus(reportLog, sb);
//		getThreadStatus(timeoutLog, sb);
//		getThreadStatus(publishLog, sb);
//		getThreadStatus(monitorLog, sb);
//		getThreadStatus(hls_Log, sb);
    }


    private static JLogBase errorLog = new JLogBase(String.format("'%s/'yyyy-MM-dd'/error.'HH'.log'", LOCAL_LOG_ROOT), new SimpleDateFormat("HH:mm:ss_SSS "));
    private static JLogBase debugLog = new JLogBase(String.format("'%s/'yyyy-MM-dd'/debug.'HH'.log'", LOCAL_LOG_ROOT), new SimpleDateFormat("HH:mm:ss_SSS "));
//	private static JLogBase cache_hit_miss = new JLogBase(String.format("'%s/'yyyy-MM-dd'/cache_hit_miss.'HH'.log'", LOCAL_LOG_ROOT), new SimpleDateFormat("HH:mm:ss_SSS "));
//	private static JLogBase runLog = new JLogBase(String.format("'%s/'yyyy-MM-dd'/run.'HH'.log'", LOCAL_LOG_ROOT), new SimpleDateFormat("HH:mm:ss_SSS "));
//	private static JLogBase syncLog = new JLogBase(String.format("'%s/'yyyy-MM-dd'/sync.'HH'.log'", LOCAL_LOG_ROOT), new SimpleDateFormat("HH:mm:ss_SSS "));
//	private static JLogBase statLog = new JLogBase(String.format("'%s/'yyyy-MM-dd'/stat.'HH'.log'", LOCAL_LOG_ROOT), new SimpleDateFormat("HH:mm:ss_SSS "));
//	private static JLogBase recvLog = new JLogBase(String.format("'%s/'yyyy-MM-dd'/recv.'HH'.log'", LOCAL_LOG_ROOT), new SimpleDateFormat("HH:mm:ss_SSS "));
//	private static JLogBase ruleLog = new JLogBase(String.format("'%s/'yyyy-MM-dd'/rule.'HH'.log'", LOCAL_LOG_ROOT), new SimpleDateFormat("HH:mm:ss_SSS "));
//	private static JLogBase downloadLog = new JLogBase(String.format("'%s/'yyyy-MM-dd'/download.'HH'.log'", LOCAL_LOG_ROOT), new SimpleDateFormat("HH:mm:ss_SSS "));
//	private static JLogBase successLog = new JLogBase(String.format("'%s/'yyyy-MM-dd'/success.'HH'.log'", LOCAL_LOG_ROOT), new SimpleDateFormat("HH:mm:ss_SSS "));
//	private static JLogBase reportLog = new JLogBase(String.format("'%s/'yyyy-MM-dd'/report.'HH'.log'", LOCAL_LOG_ROOT), new SimpleDateFormat("HH:mm:ss_SSS "));
//	private static JLogBase timeoutLog = new JLogBase(String.format("'%s/'yyyy-MM-dd'/timeout.'HH'.log'", LOCAL_LOG_ROOT), new SimpleDateFormat("HH:mm:ss_SSS "));
//	private static JLogBase publishLog = new JLogBase(String.format("'%s/'yyyy-MM-dd'/publish.'HH'.log'", LOCAL_LOG_ROOT), new SimpleDateFormat("HH:mm:ss_SSS "));
//	private static JLogBase centerAndCacheOutLog = new JLogBase(String.format("'%s/'yyyy-MM-dd'/cache_center_out.'HH'.log'", LOCAL_LOG_ROOT), new SimpleDateFormat("HH:mm:ss_SSS "));
//	private static JLogBase bandwidthLog = new JLogBase(String.format("'%s/'yyyy-MM-dd'/bandwidth.'HH'.log'", LOCAL_LOG_ROOT), new SimpleDateFormat("HH:mm:ss_SSS "));
//	private static JLogBase monitorLog = new JLogBase(String.format("'%s/'yyyy-MM-dd'/montior.'HH'.log'", LOCAL_LOG_ROOT), new SimpleDateFormat("HH:mm:ss_SSS "));
//	private static JLogBase hls_Log = new JLogBase(String.format("'%s/'yyyy-MM-dd'/hls_log.'HH'.log'", LOCAL_LOG_ROOT), new SimpleDateFormat("HH:mm:ss_SSS "));
//	private static JLogBase zookeeper_Log = new JLogBase(String.format("'%s/'yyyy-MM-dd'/zookeeper.'HH'.log'", LOCAL_LOG_ROOT), new SimpleDateFormat("HH:mm:ss_SSS "));
//	private static JLogBase deleteZookeeper_Log = new JLogBase(String.format("'%s/'yyyy-MM-dd'/delete_zookeeper.'HH'.log'", LOCAL_LOG_ROOT), new SimpleDateFormat("HH:mm:ss_SSS "));

    /**
     * 将信息从标准输出输出
     */
    private static JLogBase print = new JLogBase(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss_SSS "));

    static {
        debugLog.setParentClassname(LOG.class.getName());
        errorLog.setParentClassname(LOG.class.getName());
//		runLog.setParentClassname(LOG.class.getName());
//		cache_hit_miss.setParentClassname(LOG.class.getName());
        print.setParentClassname(LOG.class.getName());
//		syncLog.setParentClassname(LOG.class.getName());
//		statLog.setParentClassname(LOG.class.getName());
//		recvLog.setParentClassname(LOG.class.getName());
//		downloadLog.setParentClassname(LOG.class.getName());
//		ruleLog.setParentClassname(LOG.class.getName());
//		successLog.setParentClassname(LOG.class.getName());
//		reportLog.setParentClassname(LOG.class.getName());
//		timeoutLog.setParentClassname(LOG.class.getName());
//		publishLog.setParentClassname(LOG.class.getName());
//		centerAndCacheOutLog.setParentClassname(LOG.class.getName());
//		bandwidthLog.setParentClassname(LOG.class.getName());
//		monitorLog.setParentClassname(LOG.class.getName());
//		hls_Log.setParentClassname(LOG.class.getName());
//		zookeeper_Log.setParentClassname(LOG.class.getName());
//		deleteZookeeper_Log.setParentClassname(LOG.class.getName());
    }

    private static void disableTraceInfo(JLogBase log) {
        log.setShowClassname(false);
        log.setShowFilename(false);
        log.setShowLineNo(false);
    }
    /**
     * 不生成路径
     * @auther fengruiqi
     * 创建时间 2017年6月27日 下午4:06:34
     * p_log
     * @param log
     */
    private void disableTraceInfoLog(JLogBase log){
        log.setShowClassname(false);
        log.setShowFilename(false);
        log.setShowLineNo(false);
    }
    public static void disableRunTraceInfo() {
//		disableTraceInfo(log);
//		disableTraceInfo(runLog);
//		disableTraceInfo(cache_hit_miss);
//		disableTraceInfo(syncLog);
//		disableTraceInfo(statLog);
//		disableTraceInfo(recvLog);
//		disableTraceInfo(downloadLog);
//		disableTraceInfo(ruleLog);
//		disableTraceInfo(successLog);
//		disableTraceInfo(reportLog);
//		disableTraceInfo(timeoutLog);
//		disableTraceInfo(publishLog);
//		disableTraceInfo(centerAndCacheOutLog);
//		disableTraceInfo(bandwidthLog);
//		disableTraceInfo(monitorLog);
//		disableTraceInfo(hls_Log);
    }

    public static void enableRunTraceInfo(JLogBase log) {
        log.setShowClassname(true);
        log.setShowFilename(true);
        log.setShowLineNo(true);
    }

    public static void enableRunTraceInfo() {
//		enableRunTraceInfo(log);
//		enableRunTraceInfo(runLog);
//		enableRunTraceInfo(cache_hit_miss);
//		enableRunTraceInfo(syncLog);
//		enableRunTraceInfo(statLog);
//		enableRunTraceInfo(recvLog);
//		enableRunTraceInfo(downloadLog);
//		enableRunTraceInfo(ruleLog);
//		enableRunTraceInfo(successLog);
//		enableRunTraceInfo(reportLog);
//		enableRunTraceInfo(timeoutLog);
//		enableRunTraceInfo(publishLog);
//		enableRunTraceInfo(centerAndCacheOutLog);
//		enableRunTraceInfo(bandwidthLog);
//		enableRunTraceInfo(monitorLog);
//		enableRunTraceInfo(hls_Log);
    }
    /**
     * 生成运行路径
     * @auther fengruiqi
     * 创建时间 2017年6月27日 下午4:04:00
     * p_log
     */
    public void enableRunTraceInfoLog(JLogBase log){
        log.setShowClassname(true);
        log.setShowFilename(true);
        log.setShowLineNo(true);
    }

//	public static void savehlsLog(Object... msgs) {
//		hls_Log.trace("", msgs);
//		if (output2Stdout) {
//			print.print("", msgs);
//		}
//	}

    public static void error(Object... msgs) {
        errorLog.trace("[ERROR] ", msgs);
        if (output2Stdout) {
            print.print("", msgs);
        }
    }

    public static void warn(Object... msgs) {
        errorLog.trace("[WARN] ", msgs);
        if (output2Stdout) {
            print.print("", msgs);
        }
    }

    public static void debug(Object... msgs) {
        if (debug) {
            debugLog.trace("", msgs);
        }
        if (output2Stdout) {
            print.print("", msgs);
        }
    }

    public static boolean isDebug() {
        return debug;
    }

    public static void print(Object... msgs) {
        if (output2Stdout) {
            print.print("", msgs);
        }
    }

    public static void setDebug(boolean debug) {
        if (debug) {
            debugLog.setShowClassname(true);
            debugLog.setShowFilename(true);
            debugLog.setShowLineNo(true);

        } else {
            debugLog.setShowClassname(false);
            debugLog.setShowFilename(false);
            debugLog.setShowLineNo(false);
        }
        LOG.debug = debug;
    }

//	public static void saveRunLog(Object... msgs) {
//		runLog.trace("", msgs);
//		if (output2Stdout) {
//			print.print("", msgs);
//		}
//	}
//
//	public static void saveZookeeperLog(Object... msgs) {
//		zookeeper_Log.trace("", msgs);
//		if (output2Stdout) {
//			print.print("", msgs);
//		}
//	}
//
//	public static void saveDeleteZookeeperLog(Object... msgs) {
//		deleteZookeeper_Log.trace("", msgs);
//		if (output2Stdout) {
//			print.print("", msgs);
//		}
//	}

    // public static void saveCacheHitMissLog(Object... msgs) {
    // cache_hit_miss.trace("", msgs);
    // if (output2Stdout) {
    // print.print("", msgs);
    // }
    // }

    // 不打印前缀
//	public static void saveCacheHitMissLog(String msg) {
//		cache_hit_miss.trace(msg);
//		if (output2Stdout) {
//			print.print("", msg);
//		}
//	}
//
//	public static void saveSyncLog(Object... msgs) {
//		syncLog.trace("", msgs);
//		if (output2Stdout) {
//			print.print("", msgs);
//		}
//	}
//
//	public static void saveStatLog(Object... msgs) {
//		statLog.trace("", msgs);
//		if (output2Stdout) {
//			print.print("", msgs);
//		}
//	}
//
//	public static void saveRecvLog(Object... msgs) {
//		recvLog.trace("", msgs);
//		if (output2Stdout) {
//			print.print("", msgs);
//		}
//	}
//
//	public static void saveDownLoadLog(Object... msgs) {
//		downloadLog.trace("", msgs);
//		if (output2Stdout) {
//			print.print("", msgs);
//		}
//	}
//
//	public static void saveRule(Object... msgs) {
//		ruleLog.trace("", msgs);
//		if (output2Stdout) {
//			print.print("", msgs);
//		}
//	}
//
//	public static void saveReportLog(Object... msgs) {
//		reportLog.trace("", msgs);
//		if (output2Stdout) {
//			print.print("", msgs);
//		}
//	}
//
//	public static void saveTimeOutLog(Object... msgs) {
//		timeoutLog.trace("", msgs);
//		if (output2Stdout) {
//			print.print("", msgs);
//		}
//	}
//
//	public static void saveSuccessLog(Object... msgs) {
//		successLog.trace("", msgs);
//		if (output2Stdout) {
//			print.print("", msgs);
//		}
//	}
//
//	public static void savePublishLog(Object... msgs) {
//		publishLog.trace("", msgs);
//		if (output2Stdout) {
//			print.print("", msgs);
//		}
//	}
//
//	public static void saveCenterAndCacheOutLog(String msg) {
//		centerAndCacheOutLog.trace(msg);
//		if (output2Stdout) {
//			print.print("", msg);
//		}
//	}
//
//	public static void saveBandwidthLog(String msg) {
//		bandwidthLog.trace(msg);
//		if (output2Stdout) {
//			print.print("", msg);
//		}
//	}
//
//	public static void saveMonitorLog(String msg) {
//		monitorLog.trace(msg);
//		if (output2Stdout) {
//			print.print("", msg);
//		}
//	}

    /**
     * 将异常的信息转换为字符串
     *
     * @param ex
     * @return
     */
    public static String getTrace(Throwable ex) {
        StringWriter stringWriter = new StringWriter();
        PrintWriter printWriter = new PrintWriter(stringWriter);
        ex.printStackTrace(printWriter);
        return stringWriter.getBuffer().toString();
    }

    /**
     * 保存日志
     * @auther fengruiqi
     * 创建时间 2017年6月26日 上午11:49:34
     * @param msg  存入的内容
     * @param logName 日志的名字前缀
     */
    public static  void saveLog(String logName,String msg){

        JLogBase  base=null;
        if(mapName.containsKey(logName)){
            base=mapName.get(logName);
            base.trace("",msg);
            if(output2Stdout){
                print.print("", msg);
            }
        }else{
            base = new JLogBase(String.format("'%s/'yyyy-MM-dd'/"+logName+".'HH'.log'", LOCAL_LOG_ROOT), new SimpleDateFormat("HH:mm:ss_SSS "));
            mapName.put(logName, base);
            base.trace("",msg);
            if(output2Stdout){
                print.print("", msg);
            }

        }

    }

}
